package com.bee.beemanage.system.common.utils;

import cn.hutool.core.util.IdUtil;
import com.bee.beemanage.api.controller.SysDepartmentController;
import com.bee.beemanage.api.controller.SysDictController;
import com.bee.beemanage.api.controller.common.LogController;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 组织权限脚本
 */
public class DmlUtil {
    public static void main(String[] args) {

        Class<SysDepartmentController> clazz = SysDepartmentController.class;

        // 获取类中所有的方法
        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            Annotation[] annotations = method.getAnnotations();
            if (annotations == null || annotations.length == 0) {
                continue;
            }
            String insertSql = "INSERT INTO beesingle.sys_permission (id,label,code,create_user,create_time,update_user,update_time,remark,order_num,is_deleted) VALUES\n" +
                    "\t ('v1', 'v2', 'v3', 'default', '2023-06-15 12:18:17', NULL, NULL, '初始化权限码', NULL, 0);";
            String code = "";
            String label = "";
            for (Annotation annotation : annotations) {
                if (annotation instanceof PreAuthorize) {
                    // 获取注解中的值
                    String value = ((PreAuthorize) annotation).value();
                    value = value.replaceAll("hasAnyAuthority", "")
                                 .replaceAll("\\(", "")
                                 .replaceAll("\\)", "")
                                 .replaceAll("'", "");
                    code = value;
                }
                if (annotation instanceof ApiOperation) {
                    // 获取注解中的值
                    label = ((ApiOperation) annotation).value();
                }
            }
            if (!code.isEmpty() && !label.isEmpty()) {
                insertSql = insertSql.replace("v1", IdUtil.simpleUUID())
                                    .replace("v2", label)
                                    .replace("v3",code);
                System.out.println(insertSql);
            }
        }
    }
}
